#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFileDialog>
#include <QMessageBox>
#include<qglobal.h>
#include<QSlider>
#include<QSpinBox>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  this->calcu=false;
  this->highthresh=1.5;//长宽对比度
  this->lowthresh=0.4;
  this->edge=qRgb(255,255,255);
  this->not_edge=qRgb(0,0,0);
  this->resize(900,1200);
  ui->setupUi(this);

  //ui->horizontalSlider->move(50,200);//设置控件的位置
  ui->horizontalSlider->setRange(0,255);//设置数值范围
  ui->horizontalSlider->setValue(128);//设置初始值

  //ui->spinBox->move(50,100);
  ui->spinBox->setRange(0,255);
  ui->spinBox->setValue(128);
  threshold=ui->horizontalSlider->value();

  connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));//两个值同时更改
  connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));
  connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(CBinarization(int)));//调用 二值化图像

}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    qDebug()<<"aaaaaaa";
   this->highthresh=1.5;//长宽对比度
    this->lowthresh=0.4;
    double w=300,h=200;
    QString filename=QFileDialog::getOpenFileName(this,
   u8"Open File hello", QString(),
   u8"JPEG files (*.jpg *.jpeg);;PNG files (*.png)");//打开文件夹，获取文件路径
    if(!filename.isEmpty())
      {
        img.load(filename);//加载图片路径
        img=img.scaled(w,h);
        ui->label->resize(img.width(),img.height());
        ui->label->setPixmap(QPixmap::fromImage(img));

      }
    this->update();
}

void MainWindow::on_pushButton_2_clicked()
{

    img_gray=new QImage
        (img.width(),img.height(),QImage::Format_ARGB32);
    QColor old_color;
    int r,g,b;
    int avr;
    for(int i=0;i<img_gray->width();i++)
      {
        for(int j=0;j<img_gray->height();j++)
          {
            old_color=img.pixel(i,j);
            r=old_color.red();
            g=old_color.green();
            b=old_color.blue();
            avr=(r+g+b)/3;
            img_gray->setPixel(i,j,qRgb(avr,avr,avr));//设置像素点color
          }
      }
    ui->label_2->resize(img_gray->width(),img_gray->height());
    ui->label_2->setPixmap(QPixmap::fromImage(*img_gray));

}

void MainWindow::on_pushButton_3_clicked()//滤波处理
{
  int X[3][3]={{1,2,1},
               {2,4,2},
               {1,2,1}};
  double gaosi[3][3]={{0.0947416,0.118318,0.0947416},
                      {0.118318,0.147761,0.118318},
                      {0.0947416,0.118318,0.0947416}};//权重和为1

  QColor Old_color;
  double r,g,b;
  int R,G,B;
  QImage *img_nvbo=new QImage
      (img.width(),img.height(),QImage::Format_ARGB32);
  for(int y=1;y<img_nvbo->height()-1;y++)
    {
      for(int x=1;x<img_nvbo->width()-1;x++)
        {
          r=0;
          g=0;
          b=0;
          for(int j=-1;j<=1;j++)
            {
              for(int i=-1;i<=1;i++)
                {
                   Old_color=img.pixel(i+x,j+y);

                  r=r+Old_color.red()*gaosi[i+1][j+1];

                  g=g+Old_color.green()*gaosi[i+1][j+1];

                  b=b+Old_color.blue()*gaosi[i+1][j+1];///高斯模糊

                  /* r+=(Old_color.red()*X[i+1][j+1]);
                   g+=(Old_color.green()*X[i+1][j+1]);
                   b+=(Old_color.blue()*X[i+1][j+1]);*/
                }
            }
         /*r=r/16;
         g=g/16;
         b=b/16;*/
          R=(int)r;
          G=(int)g;
          B=(int)b;
          // qDebug()<<R<<G<<B;
          /*R=qBound(0,R,255);
          G=qBound(0,G,255);
          B=qBound(0,B,255);*/

        // img_nvbo->setPixel(x,y,qRgb(r,g,b));
           img_nvbo->setPixel(x,y,qRgb(R,G,B));
        }
    }
  ui->label_4->resize(img_nvbo->width(),img_nvbo->height());
  ui->label_4->setPixmap(QPixmap::fromImage(*img_nvbo));

}

void MainWindow::CBinarization(int threshold)
{
  qDebug()<<"******cb";
  img_bin=new QImage
      (img.width(),img.height(),QImage::Format_ARGB32);;
  int bt;
  QColor color;
  for(int i=0;i<img_bin->width();i++)
    {
      for(int j=0;j<img_bin->height();j++)
        {
          color=img.pixel(i,j);
          bt=color.red();

          if(bt<threshold) bt=0;
          else bt=255;

          img_bin->setPixel(i,j,qRgb(bt,bt,bt));
        }
    }
  ui->label_5->resize(img_bin->width(),img_bin->height());
  ui->label_5->setPixmap(QPixmap::fromImage(*img_bin));
}


